<?php
/**
 * vim: et ts=4 sts=4 sw=4 fdm=marker
 *
 * 测试controller 
 *
 * @author	wang Penghai <china_php@hotmail.com>
 * @copyright 2008 5e6d Corporation.
 * @version   $Id: config.php 29 2009-03-10 14:30:18Z wph $
 */
pf_load::init('admin_class_admin');
class group_action extends pf_controller{
	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		$group_obj = pf_load::init('admin_model_group');
		$argv = array(
			 'fields'=>array('id', 'name', 'access_list'),
			 //'limit' => 1,
			 //'offset' => 0,
			 'order' => 'updatetime desc',
			 //'group'=>'id'
			 //'where'=> '1=1',
		);
		$group_list = $group_obj->get_group_list($argv);
		$this->assign('group_list', $group_list);
		$this->view('admin_admin_view_group');
	}
	//添加显示页
	public function add()
	{
		$sysmenu_list = $this->get_menu_list();
		$this->assign('sysmenu_list', $sysmenu_list);
		$this->view('admin_admin_view_group.add');
	}
	private function get_menu_list($access_list = array())
	{
		$menu_obj = pf_load::init('admin_model_sysmenu');
		$menu_list = $menu_obj->get_sysmenu_list();
		$ids = array();
		$id = '';

		foreach ($menu_list as $key=>$val)
		{
			$level = $val['level'];
			$str = $id = '';
			if ($level > 1)
			{
				
				for($i=1; $i < $level; $i++)
				{
					$str .= '|&nbsp;&nbsp;';
					$id .= '_' . $ids[$i];
				}
				$str .= "└";

			} else {
				$ids = array();
			}
			$checked = '';
			if (!empty($access_list) && in_array($val['id'], $access_list))
			{
				$checked = 'checked';
			}
			$html = '<input id="menu'.$id.'_'.$val['id'].'" type="checkbox" name="menu_id[]" value="'.$val['id'].'" onclick="checknote(this);" '.$checked.'/>';
			$menu_list[$key]['cname'] = $str . $html . $menu_list[$key]['cname'];
			$ids[$level] = $val['id'];
		}
		return $menu_list;
	}

	//添加处理
	public function insert()
	{
		$groupname = $_POST['groupname'];
		$access_list = $_POST['menu_id'];
		$gid = (int)$_POST['gid'];

		$access_list = implode(',', $access_list);
		$time = time();
		$argv = array(
			'name' => $groupname,
			'access_list'=> $access_list,
			'createtime' => $time,
			'updatetime' => $time,
		);
		$group_obj = pf_load::init('admin_model_group');
		if (0 !== $gid)
		{
			$update_argv['data'] = $argv;
			unset($argv);
			$update_argv['where'] = 'id=' . $gid;
			var_export($update_argv);
			$res = $group_obj->update($update_argv);
		} else {
			$res = $group_obj->insert($argv);
		}
		if (False === $res)
		{
			echo 'error!';
			exit;
		}
		echo 'dong';
	}
	//编辑显示页
	public function edit()
	{
		$gid = $_GET['gid'];
		$group_obj = pf_load::init('admin_model_group');
		$argv = array(
			 'fields'=>array('id', 'name', 'access_list'),
			 //'limit' => 1,
			 //'offset' => 0,
			 'order' => 'updatetime desc',
			 //'group'=>'id'
			 'where'=> 'id=' . $gid,
		);
		$group_list = $group_obj->get_group_list($argv, false);
		$access_list = explode(',', $group_list['access_list']);
		$this->assign('group_list', $group_list);
		$this->assign('gid', $gid);
		$sysmenu_list = $this->get_menu_list($access_list);
		$this->assign('sysmenu_list', $sysmenu_list);
		$this->view('admin_admin_view_group.add');

	}
}
